package com.tc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tc.domain.Drug_reimbursement_ratio;
import com.tc.dto.Drug_reimbursement_ratioQueryDTO;
import com.tc.mapper.Drug_reimbursement_ratioMapper;
import com.tc.service.IDrug_reimbursement_ratioService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tc.vo.Drug_reimbursement_ratioVO;
import com.tc.vo.PageResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  药品报销比例服务实现类
 * </p>
 *
 * @author jcd
 * @since 2025-07-09
 */
@Service
public class Drug_reimbursement_ratioServiceImpl extends ServiceImpl<Drug_reimbursement_ratioMapper, Drug_reimbursement_ratio> implements IDrug_reimbursement_ratioService {

    /**
     * 分页查询药品报销比例信息
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @Override
    public PageResultVO<Drug_reimbursement_ratioVO> pageQuery(Drug_reimbursement_ratioQueryDTO queryDTO) {
        // 构建分页查询对象
        Page<Drug_reimbursement_ratio> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        
        // 构建查询条件
        LambdaQueryWrapper<Drug_reimbursement_ratio> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (StringUtils.hasText(queryDTO.getDrugType())) {
            queryWrapper.eq(Drug_reimbursement_ratio::getDrugType, queryDTO.getDrugType());
        }
        if (StringUtils.hasText(queryDTO.getStatus())) {
            queryWrapper.eq(Drug_reimbursement_ratio::getStatus, queryDTO.getStatus());
        }
        
        // 报销比例区间查询
        if (queryDTO.getMinRatio() != null) {
            queryWrapper.ge(Drug_reimbursement_ratio::getReimbursementRatio, queryDTO.getMinRatio());
        }
        if (queryDTO.getMaxRatio() != null) {
            queryWrapper.le(Drug_reimbursement_ratio::getReimbursementRatio, queryDTO.getMaxRatio());
        }
        
        // 按更新时间降序排序
        queryWrapper.orderByDesc(Drug_reimbursement_ratio::getUpdateTime);
        
        // 执行分页查询
        IPage<Drug_reimbursement_ratio> pageResult = this.page(page, queryWrapper);
        
        // 转换records
        List<Drug_reimbursement_ratioVO> voList = new ArrayList<>();
        for (Drug_reimbursement_ratio ratio : pageResult.getRecords()) {
            Drug_reimbursement_ratioVO vo = new Drug_reimbursement_ratioVO();
            BeanUtils.copyProperties(ratio, vo);
            voList.add(vo);
        }
        
        // 使用通用PageResultVO
        return PageResultVO.convert(pageResult, voList);
    }

    /**
     * 查询所有药品报销比例
     * @param status 状态，可选参数，传null表示查询所有状态
     * @return 药品报销比例列表
     */
    @Override
    public List<Drug_reimbursement_ratioVO> listAll(String status) {
        // 构建查询条件
        LambdaQueryWrapper<Drug_reimbursement_ratio> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果状态不为空，则按状态筛选，否则查询全部
        if (StringUtils.hasText(status)) {
            queryWrapper.eq(Drug_reimbursement_ratio::getStatus, status);
        }
        
        // 按药品类型和更新时间排序
        queryWrapper.orderByAsc(Drug_reimbursement_ratio::getDrugType)
                   .orderByDesc(Drug_reimbursement_ratio::getUpdateTime);
        
        // 执行查询
        List<Drug_reimbursement_ratio> ratios = this.list(queryWrapper);
        
        // 转换为VO对象
        return ratios.stream().map(ratio -> {
            Drug_reimbursement_ratioVO vo = new Drug_reimbursement_ratioVO();
            BeanUtils.copyProperties(ratio, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}
